package Classes.Managers// ActionScript file
{
	import Classes.GameUtils.GU_DataTransform;
	import Classes.GameViews.ViewCartoonTargets.ViewCartoonTarget;
	import Classes.Interfaces.I_NeedSWFSource;
	import Classes.Interfaces.I_NeedUpdatePaperMan;
	
	import MV_BMP.BMPMCEvent;
	import MV_BMP.MovieClip_BMP;
	
	import PaperMan.PaperManComponent;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.system.ApplicationDomain;

	/**
	 *一般僵尸形象和动画管理类 
	 * @author 葛增文
	 * 
	 */	
	public class M_CommonZombieCartoons extends Manager implements I_NeedSWFSource
	{
		public static const DRAWRATE:int=10;/*绘制频率（毫秒/每帧）*/
		
		private static var _instance:M_CommonZombieCartoons;/*静态引用*/
		private var _zombieSkinsFolderPath:String;
		private var needBaseCartoonsAry:Array=["zombie_small","zombie_medium","zombie_large"]
		
		private var _zombieCartoonsObj:Object;/*动画存储器*/
		private var _zombieCartoonSkinsSetObj:Object;/*动画皮肤设定存储器*/
		private var _waitingUpdateTargetsObj:Object;/*等待载入资源的对象存储器*/
		private var _waitingCreatObj:Object;/*等待创建模型的对象存储器*/
		
		
		private var _waitingCreatAry:Array;
		private var _nowCreatObj:Object;

		public function M_CommonZombieCartoons():void
		{
			_instance=this;
			this.init();
		}
		
		private function init():void
		{
			this._zombieCartoonsObj=new Object;
			this._zombieCartoonSkinsSetObj=new Object;
			this._waitingUpdateTargetsObj=new Object;
			this._waitingCreatObj=new Object;
			
			this._waitingCreatAry=new Array;
		}
	
		override public function importData():void
		{
			this._zombieSkinsFolderPath=M_ConfigData.instance.imgUrl;
			var skinsSetCSV:String=M_ConfigData.instance.getCSVByName("僵尸模型皮肤配置表");
			var skinsSetAry:Array=GU_DataTransform.csvTransformObjsAry(skinsSetCSV,44);
			for(var i:int=0;i<skinsSetAry.length;i++)
			{
				var skinSetDataObj:Object=skinsSetAry[i];
				if(!this.zombieCartoonSkinsSetObj.hasOwnProperty(skinSetDataObj.zombieType))
				{
					this.zombieCartoonSkinsSetObj[skinSetDataObj.zombieType]=new Object;
					this.zombieCartoonSkinsSetObj[skinSetDataObj.zombieType].baseCartoonType=skinSetDataObj.baseCartoonType;
					this.zombieCartoonSkinsSetObj[skinSetDataObj.zombieType].skinsSource=skinSetDataObj.skinsSource;
					this.zombieCartoonSkinsSetObj[skinSetDataObj.zombieType].cartoonSkins=new Object;
				}
				this.zombieCartoonSkinsSetObj[skinSetDataObj.zombieType].cartoonSkins[skinSetDataObj.actionName]=skinSetDataObj;
			}
			this.loadBaseCartoons();
		}
		
		/**
		 *载入裸模动画 
		 * 
		 */		
		private function loadBaseCartoons():void
		{
			if(this.needBaseCartoonsAry.length)
			{
				var nowNeed:String=this.needBaseCartoonsAry.shift();
				M_BaseCartoons.instance.needLoadBaseCartoon(nowNeed,creatBaseCartoons);
			}
			else
			{
				this.importOver(2);
			}
		}
		
		/**
		 *创建裸模动画 
		 * @param baseCartoonType
		 * 
		 */
		private function creatBaseCartoons(baseCartoonType:String):void
		{
			this.zombieCartoonsObj[baseCartoonType]=new Object;
			var baseCartoonObj:Object=M_BaseCartoons.instance.getBaseCartoonObjByType(baseCartoonType);
			this.zombieCartoonsObj[baseCartoonType].actions=new Object;
			for(var actionName:String in baseCartoonObj.actions)
			{
				var actionObj:Object=baseCartoonObj.actions[actionName];
				var creatMC:MovieClip=(new (ApplicationDomain.currentDomain.getDefinition(actionObj.className) as Class)) as MovieClip;
				var creatBMPMC:MovieClip_BMP=new MovieClip_BMP;
				creatMC.scaleX=ViewCartoonTarget.scaleValue;
				creatMC.scaleY=ViewCartoonTarget.scaleValue;
				creatBMPMC.turnFrom(creatMC);
				this.zombieCartoonsObj[baseCartoonType].actions[actionName]={cartoon:creatBMPMC,setX:actionObj.setX*ViewCartoonTarget.scaleValue,setY:actionObj.setY*ViewCartoonTarget.scaleValue};
			}
			this.loadBaseCartoons();
		}
		
		/**
		 *直接获取动画包 
		 * @param zombieType 僵尸类型
		 * @return 
		 * 
		 */		
		private function getCartoonObj(zombieType:String):Object
		{
			var returnObj:Object=new Object;
			var zombieCartoonObj:Object=this.zombieCartoonsObj[zombieType].actions;
			for(var cartoonName:String in zombieCartoonObj)
			{
				var cartoonObj:Object=zombieCartoonObj[cartoonName];
				returnObj[cartoonName]={cartoon:(cartoonObj.cartoon as MovieClip_BMP).clone(),setX:cartoonObj.setX,setY:cartoonObj.setY};
			}
			return returnObj;
		}
		
		/**
		 *获取僵尸动画包 
		 * @param zombieType 僵尸类型
		 * @param zombie 获取僵尸动画的对象
		 * @return 
		 * 
		 */		
		public function getZombieCartoonsObj(zombieType:String,zombie:I_NeedUpdatePaperMan):Object
		{
			if(this.zombieCartoonsObj.hasOwnProperty(zombieType))
			{
				return this.getCartoonObj(zombieType);
			}
			if(this.nowCreatObj && this.nowCreatObj.zombieType==zombieType)
			{
				(this.nowCreatObj.needUpdateAry as Array).push(zombie);
			}
			else
			{
				var waitCreatObj:Object;
				for each(var checkCreatObj:Object in this.waitingCreatAry)
				{
					if(checkCreatObj.zombieType==zombieType)
					{
						waitCreatObj=checkCreatObj;
						break;
					}
				}
				if(!waitCreatObj)
				{
					waitCreatObj=new Object;
					waitCreatObj.zombieType=zombieType;
					waitCreatObj.needUpdateAry=new Array;
					this.waitingCreatAry.push(waitCreatObj);
					this.beginCreat();
				}
				(waitCreatObj.needUpdateAry as Array).push(zombie);
			}
			var zombieBaseCartoonType:String=this.zombieCartoonSkinsSetObj[zombieType].baseCartoonType;
			return this.getCartoonObj(zombieBaseCartoonType);
		}
		
		/**
		 *开始创建当前的僵尸动画包 
		 * 
		 */
		private function beginCreat():void
		{
			if(this.nowCreatObj || !this.waitingCreatAry.length)return;
			this.nowCreatObj=this.waitingCreatAry.shift();
			var skinSetObj:Object=this.zombieCartoonSkinsSetObj[this.nowCreatObj.zombieType];
			var sourceName:String=skinSetObj.skinsSource;
			if(M_LoadSWFSource.instance.checkSource(sourceName))
			{
				this.creatActions();
			}
			else
			{
				var loadPath:String=this.zombieSkinsFolderPath+sourceName;
				M_LoadSWFSource.instance.needLoad(this,loadPath,sourceName);
			}
		}
		
		/**
		 *开始创建当前的僵尸行为动画 
		 * 
		 */
		private function creatActions():void
		{
			this.nowCreatObj.waitingCreatActionsAry=new Array;
			this.nowCreatObj.cartoonsObj=new Object;
			var skinSetObj:Object=this.zombieCartoonSkinsSetObj[this.nowCreatObj.zombieType];
			var skinsObj:Object=skinSetObj.cartoonSkins;
			for(var cartoonName:String in skinsObj)
			{
				(this.nowCreatObj.waitingCreatActionsAry as Array).push(cartoonName);
			}
			this.creatAction();
		}
		
		/**
		 *开始创建当前的僵尸某个行为动画 
		 * 
		 */
		private function creatAction():void
		{
			var nowAry:Array=this.nowCreatObj.waitingCreatActionsAry;
			if(nowAry.length)
			{
				var nowCreatActionName:String=nowAry.shift();
				var skinSetObj:Object=this.zombieCartoonSkinsSetObj[this.nowCreatObj.zombieType];
				var baseCartoonObj:Object=M_BaseCartoons.instance.getBaseCartoonObjByType(skinSetObj.baseCartoonType).actions[nowCreatActionName];
				if(!baseCartoonObj)throw new Error("裸模不存在");
				var zombiePaperMan:PaperManComponent=new PaperManComponent;
				zombiePaperMan.scaleMotionX=ViewCartoonTarget.scaleValue;
				zombiePaperMan.scaleMotionY=ViewCartoonTarget.scaleValue;
				zombiePaperMan.motionName=baseCartoonObj.className;
				for(var i:int=0;i<40;i++)
				{
					var partStr:String="part"+i+"Str";
					var skinClassName:String=skinSetObj.cartoonSkins[nowCreatActionName][partStr];
					if(skinClassName && skinClassName!="")
					{
						zombiePaperMan[partStr]=skinClassName;
					}
				}
				zombiePaperMan.drawRate=DRAWRATE;
				this.nowCreatObj.cartoonsObj[nowCreatActionName]={cartoon:zombiePaperMan,setX:baseCartoonObj.setX*ViewCartoonTarget.scaleValue,setY:baseCartoonObj.setY*ViewCartoonTarget.scaleValue};
				zombiePaperMan.addEventListener(BMPMCEvent.TURNOVER,turnOverHandler);
				zombiePaperMan.drawNow();
			}
			else
			{
				this.creatOver();
			}
		}
		
		/**
		 *当前僵尸的动画包创建完成 
		 * 
		 */
		private function creatOver():void
		{
			var zombieType:String=this.nowCreatObj.zombieType;
			this.zombieCartoonsObj[zombieType]={actions:new Object};
			for(var cartoonName:String in this.nowCreatObj.cartoonsObj)
			{
				this.zombieCartoonsObj[zombieType].actions[cartoonName]=this.nowCreatObj.cartoonsObj[cartoonName];
			}
			var needUpdateAry:Array=this.nowCreatObj.needUpdateAry;
			this.nowCreatObj=null;
			for each(var needUpdateTarget:I_NeedUpdatePaperMan in needUpdateAry)
			{
				needUpdateTarget.updatePaperMan();
			}
			this.beginCreat();
		}
		
		/**
		 *动画转换完成 
		 * @param e
		 * 
		 */		
		private function turnOverHandler(e:BMPMCEvent):void
		{
			(e.target as EventDispatcher).removeEventListener(BMPMCEvent.TURNOVER,turnOverHandler);
			this.creatAction();
		}
		
		/**
		 *皮肤swf资源载入完成 
		 * 
		 */		
		private function skinsSourceGetOver(sourceName:String):void
		{
			this.creatActions();
		}
		
		/**
		 *载入完成事件 
		 * @param sourceName
		 * 
		 */		
		public function sourceLoadOver(sourceName:String):void
		{
			this.skinsSourceGetOver(sourceName);
		}

		/*getter & setter*/
		public static function get instance():M_CommonZombieCartoons
		{
			return _instance;
		}

		public function get zombieCartoonsObj():Object
		{
			return _zombieCartoonsObj;
		}

		public function get zombieCartoonSkinsSetObj():Object
		{
			return _zombieCartoonSkinsSetObj;
		}

		public function get waitingUpdateTargetsObj():Object
		{
			return _waitingUpdateTargetsObj;
		}

		public function get zombieSkinsFolderPath():String
		{
			return _zombieSkinsFolderPath;
		}

		public function get waitingCreatObj():Object
		{
			return _waitingCreatObj;
		}

		public function set waitingCreatObj(value:Object):void
		{
			_waitingCreatObj = value;
		}

		public function get waitingCreatAry():Array
		{
			return _waitingCreatAry;
		}

		public function set waitingCreatAry(value:Array):void
		{
			_waitingCreatAry = value;
		}

		public function get nowCreatObj():Object
		{
			return _nowCreatObj;
		}

		public function set nowCreatObj(value:Object):void
		{
			_nowCreatObj = value;
		}
		
		
		/**
		 *返回动画的各部件 
		 * @param ztype
		 * @param actionName
		 * @param partName
		 * @return 
		 * 
		 */
		public function getPart(ztype:String,actionName:String,partName:String):DisplayObjectContainer
		{
			var baseCartoonObj:Object=M_BaseCartoons.instance.getBaseCartoonObjByType(this.zombieCartoonSkinsSetObj[ztype].baseCartoonType);
			var actionObj:Object=baseCartoonObj.actions[actionName];
			var creatMC:MovieClip=(new (ApplicationDomain.currentDomain.getDefinition(actionObj.className) as Class)) as MovieClip;
			creatMC.scaleX=ViewCartoonTarget.scaleValue;
			creatMC.scaleY=ViewCartoonTarget.scaleValue;
			
			
			
			return creatMC.getChildByName(partName) as DisplayObjectContainer;
		}
	}
}